<?php

namespace App\Http\Controllers\OperationManagement;

use App\Models\Landscaping\ParkVegetation;
use App\Models\Landscaping\Vegetation;
use App\Models\OperationManagement\GovernmentDevice;
use App\Models\OperationManagement\GovernmentDeviceCategory;
use App\Models\OperationManagement\JobScope;
use App\Models\OperationManagement\SanitationCategory;
use App\Models\OperationManagement\SanitationSite;
use App\Models\OperationManagement\SanitationToilet;
use App\Models\OperationManagement\TrashSite;
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * @group 基础信息管理->环卫->作业区域设置->地图划定区域内信息统计
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */

class SanitationStatisticsController extends Controller
{

    /**
     * 运营管理->环卫->作业区域设置->工作模拟
     *
     * @bodyParam areas polygon required 区域面积
     *
     * @queryParam sanitation 站点统计信息
     * @queryParam governmentDevice 市政设施统计信息
     * @queryParam trashInfo 垃圾点位统计信息
     * @queryParam trashTotal 垃圾点位数量统计信息
     * @queryParam toiletInfo 公厕点位统计信息
     * @queryParam roadInfo 道路统计信息
     * @queryParam roadAreasTotal 道路总面积统计信息
     * @queryParam roadJobAreas 道路作业面积统计信息
     *
     * @return \Illuminate\Http\Response
     */
    public function operationTotal(Request $request)
    {
        $areas = $request->input('areas');
        //站点统计
        $sanitationCategory = SanitationCategory::get(['id','name'])->toArray();
        $sanitation = [];
        foreach ($sanitationCategory as $v) {
            $sanitation[$v['name']] = SanitationSite::where(function($query) use ($areas){
                    if(!empty($areas)) {
                        $areas = Polygon::fromString($areas);
                        $query->within('location',$areas);
                    }
                })
                ->where('category_id',$v['id'])
                ->count();
        }

        //市政设施统计
        $governmentDeviceCategory = GovernmentDeviceCategory::get(['id','name','property'])->toArray();
        $governmentDevice = [];
        foreach ($governmentDeviceCategory as $v) {
            if($v['property'] == '0') {
                $governmentDevice[$v['name']] = GovernmentDevice::where(function($query) use ($areas){
                        if(!empty($areas)) {
                            $areas = Polygon::fromString($areas);
                            $query->within('location',$areas);
                        }
                    })
                    ->where('device_type_id',$v['id'])
                    ->count();
            } elseif ($v['property'] == '1') {
                $governmentDevice[$v['name']] = GovernmentDevice::where(function($query) use ($areas){
                        if(!empty($areas)) {
                            $areas = Polygon::fromString($areas);
                            $query->within('areas',$areas);
                        }
                    })
                    ->where('device_type_id',$v['id'])
                    ->count();
            }

        }

        $trashSiteCategory = array(
            '1' => '有害垃圾',
            '2' => '可回收垃圾',
            '3' => '干垃圾',
            '4' => '湿垃圾',
        );
        //垃圾点位统计
        $trashTotal = TrashSite::where(function($query) use ($areas){
                        if(!empty($areas)) {
                            $areas = Polygon::fromString($areas);
                            $query->within('location',$areas);
                        }
                    })->count();
        $trashSite = [];//垃圾桶数量统计
        foreach ($trashSiteCategory as $k=>$v) {
            $trashSite[$v] = TrashSite::where(function($query) use ($areas){
                            if(!empty($areas)) {
                                $areas = Polygon::fromString($areas);
                                $query->within('location',$areas);
                            }
                        })
                        ->where('type',$k)
                        ->sum('number');
        }
        //垃圾点位信息统计
        $trashInfo = array(
            'trashTotal' => $trashTotal,
            'trashSite' => $trashSite
        );

        //公厕点位统计
        $toiletInfo['toilet'] = SanitationToilet::where(function($query) use ($areas){
                                if(!empty($areas)) {
                                    $areas = Polygon::fromString($areas);
                                    $query->within('location',$areas);
                                }
                            })
                            ->count();

        //道路数据统计
        //道路长度统计
        $roadLengthTotal = JobScope::where(function($query) use ($areas){
                            if(!empty($areas)) {
                                $areas = Polygon::fromString($areas);
                                $query->within('areas',$areas);
                            }
                        })
                        ->sum('road_length');
        //道路宽度统计
        $roadWidthTotal = JobScope::where(function($query) use ($areas){
                            if(!empty($areas)) {
                                $areas = Polygon::fromString($areas);
                                $query->within('areas',$areas);
                            }
                        })
                        ->sum('road_width');
        //道路总面积
        $roadAreasTotal = $roadLengthTotal * $roadWidthTotal;
        //不同等级道路的总作业面积统计
        $road_level = ["一级"=>1,"二级"=>2,"三级"=>3];//道路等级
        $roadJobAreas = [];//作业面积
        foreach ($road_level as $k=>$v) {
            $roadJobAreas[$k] = JobScope::where(function($query) use ($areas){
                        if(!empty($areas)) {
                            $areas = Polygon::fromString($areas);
                            $query->within('areas',$areas);
                        }
                    })
                    ->where('road_level',$v)
                    ->sum('road_areas');
        }
        //道路信息统计
        $roadInfo = array(
            'roadAreasTotal' => $roadAreasTotal,
            'roadJobAreas' => $roadJobAreas
        );

        //区域内所有信息汇总
        $areaInfo = array(
            'sanitation' => $sanitation,
            'governmentDevice' => $governmentDevice,
            'trashInfo' => $trashInfo,
            'toiletInfo' => $toiletInfo,
            'roadInfo' => $roadInfo
        );
        // dd($areaInfo);
        return response()->json(encodeMsg($areaInfo,200));
    }

    /**
     * 运营管理->绿化->绿化基础数据图->工作模拟
     *
     * @bodyParam areas polygon required 区域面积
     *
     * @queryParam sanitation 站点统计信息
     *
     * @return \Illuminate\Http\Response
     */
    public function vegetationTotal(Request $request)
    {
        $areas = $request->input('areas');
        $vegetation = Vegetation::get(['id','name','property'])->toArray();
        $parkVegetation = [];
        foreach ($vegetation as $v) {
            if($v['property'] == '0') {
                $parkVegetation[$v['name']] = ParkVegetation::where(function ($query) use ($areas) {
                                if (!empty($areas)) {
                                    $areas = Polygon::fromString($areas);
                                    $query->within('location', $areas);
                                }
                            })
                            ->where('vegetation_id', $v['id'])
                            ->count();
            } else if($v['property'] == '1') {
                $parkVegetation[$v['name']] = ParkVegetation::where(function ($query) use ($areas) {
                                if (!empty($areas)) {
                                    $areas = Polygon::fromString($areas);
                                    $query->within('areas', $areas);
                                }
                            })
                            ->where('vegetation_id', $v['id'])
                            ->count();
            } else if($v['property'] == '0,1') {
                $data = ParkVegetation::where('location','!=','')->count();
                if($data) {
                    $locationCount = ParkVegetation::where(function ($query) use ($areas) {
                            if (!empty($areas)) {
                                $areas = Polygon::fromString($areas);
                                $query->within('location', $areas);
                            }
                        })
                        ->where('vegetation_id', $v['id'])
                        ->count();
                }
                $data = ParkVegetation::where('areas','!=','')->count();
                if($data) {
                    $areasCount = ParkVegetation::where(function ($query) use ($areas) {
                            if (!empty($areas)) {
                                $areas = Polygon::fromString($areas);
                                $query->within('areas', $areas);
                            }
                        })
                        ->where('vegetation_id', $v['id'])
                        ->count();
                }
                $parkVegetation[$v['name']] = $locationCount + $areasCount;
            }
        }

        return response()->json(encodeMsg($parkVegetation,200));
    }
}
